package com.boyu.common;

import com.boyu.common.model.Column;
import com.boyu.common.showmodel.ShowColumn;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2016/8/30.
 */
public class ColumnLogic {
	private static ColumnLogic instance = null;

	private ColumnLogic() {
	}

	public static ColumnLogic getInstance() {
		if (instance == null) {
			instance = new ColumnLogic();
		}
		return instance;
	}



	public List<ShowColumn> columnLoad() {
		List<Column> allColumnList = Column.dao.find("select * from t_column");
		List<ShowColumn> showColumnList = new ArrayList<>();
		List<Column> parentList = allColumnList.stream().filter(menu -> menu.getParentId() == 0).collect(Collectors.toList());
		for (Column menu : parentList) {
			ShowColumn parentShowColumn = new ShowColumn(menu);
			showColumnList.add(parentShowColumn);
			addChildNode(allColumnList,parentShowColumn);
		}
		return showColumnList;
	}

	public void addChildNode(List<Column> list, ShowColumn parentShowColumn) {
		if (list == null) {
			return;
		}
		List<Column> parentList = list.stream().filter(column -> column.getParentId() == parentShowColumn.getColumn().getId()).collect(Collectors.toList());
		for (Column column : parentList) {
			ShowColumn subColumn = new ShowColumn(column);
			subColumn.setParentColumn(parentShowColumn.getColumn());
			parentShowColumn.addSubShowColumn(subColumn);
			addChildNode(list,subColumn);
		}
	}
}
